package com.ff.xyh.service.post.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ff.xyh.common.entity.post.PostShop;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.servicebase.exception.MyException;
import com.ff.xyh.common.qo.post.PostTravelQuery;
import com.ff.xyh.common.entity.post.PostTravel;
import com.ff.xyh.service.post.mapper.PostTravelMapper;
import com.ff.xyh.service.post.service.PostTravelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 旅游凑单表 服务实现类
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@Service
public class PostTravelServiceImpl extends ServiceImpl<PostTravelMapper, PostTravel> implements PostTravelService {

    @Override
    public ResultObject getPostTravelPageCondition(PostTravelQuery postTravelQuery) {

        Page<PostTravel> page = new Page<>(postTravelQuery.getCurrentPage(), postTravelQuery.getPageSize());
        List<PostTravel> resultList = new ArrayList<>();
        QueryWrapper<PostTravel> wrapper = new QueryWrapper<>();


        // 如果通过id查询 那就只需要查询id 然后直接封装返回
        if (!StringUtils.isEmpty(postTravelQuery.getId())) {
            PostTravel getById = this.getById(postTravelQuery.getId());
            if (getById != null)
                resultList.add(getById);
            return ResultObject.ok().putList(resultList, resultList.size());
        }
        // 不为空 拼接上
        if (!StringUtils.isEmpty(postTravelQuery.getContains())) {
            wrapper.and(w ->
                    w.like("name", postTravelQuery.getContains())
                            .or().like("content",postTravelQuery.getContains()));
        }


        if (postTravelQuery.getPlatformId() != null) {
            wrapper.eq("platform_id", postTravelQuery.getPlatformId());
        }
        if (postTravelQuery.getPostStatus() != null) {
            wrapper.eq("post_status", postTravelQuery.getPostStatus());
        }
        if (postTravelQuery.getCheckStatus() != null) {
            wrapper.eq("check_status", postTravelQuery.getCheckStatus());
        }

        if (postTravelQuery.getMaxConsumePrice() != null) {
            wrapper.le("consume_price", postTravelQuery.getMaxConsumePrice());
        }
        if (postTravelQuery.getMinConsumePrice() != null) {
            wrapper.ge("consume_price", postTravelQuery.getMinConsumePrice());
        }

        if (postTravelQuery.getTotalMax() != null) {
            wrapper.le("total_max", postTravelQuery.getTotalMax());
        }
        if (postTravelQuery.getTotalMin() != null) {
            wrapper.ge("total_min", postTravelQuery.getTotalMin());
        }

        if (postTravelQuery.getEarliestTime() != null) {
            wrapper.ge("gmt_setoff", postTravelQuery.getEarliestTime());
        }
        if (postTravelQuery.getLatestTime() != null) {
            wrapper.le("gmt_setoff", postTravelQuery.getLatestTime());
        }

        if (postTravelQuery.getGmtCreatedAfter() != null) {
            wrapper.ge("gmt_created", postTravelQuery.getGmtCreatedAfter());
        }
        if (postTravelQuery.getGmtCreatedBefore() != null) {
            wrapper.le("gmt_created", postTravelQuery.getGmtCreatedBefore());
        }


        // 按修改时间排序意味着要最新修改
        if (postTravelQuery.getOrderByModifiedTime() != null && postTravelQuery.getOrderByModifiedTime()) {
            // 降序
            wrapper.orderByDesc("gmt_setoff");
        } else {
            wrapper.orderByAsc("gmt_modified");
        }

        // 按创建时间排序意味着要最新
        if (postTravelQuery.getOrderByCreatedTime() != null && postTravelQuery.getOrderByCreatedTime()) {
            // 降序
            wrapper.orderByDesc("gmt_created");
        } else {
            wrapper.orderByAsc("gmt_created");
        }

        IPage<PostTravel> resultPage = this.page(page, wrapper);

        return ResultObject.ok().putList(resultPage.getRecords(), resultPage.getTotal());
    }

    @Override
    public PostTravel insertPostTravel(PostTravel postTravel, int level) {
        isArgumentsIllegal(postTravel, level);
        int insert = baseMapper.insert(postTravel);
        System.out.println("Insert" + postTravel);
        System.out.println("Insert Result:" + insert);
        if (insert > 0)
            return postTravel;
        else
            return null;
    }

    private void isArgumentsIllegal(PostTravel postTravel, int level) {
        int costLevel = 100;
        int costLevel1 = 200;
        int costLevel2 = 500;
        int costLevel3 = 1000;
//        if (postTravel.getTotalMax() > 20)
//            throw new MyException(20001, "最大人数不能小于最小人数");
        if (postTravel.getTotalMax() < postTravel.getTotalMin())
            throw new MyException(20001, "最大人数不能小于最小人数");
        if ((postTravel.getTotalMin() - postTravel.getUserNumber()) < 2)
            throw new MyException(20001, "其他参与人数最低不能少于2人");
        if (postTravel.getGmtEffective().getTime() > postTravel.getGmtSetoff().getTime())
            throw new MyException(20001, "有效时间不能晚于出发时间");


        // 消费等级
        switch (level) {
            case 1 :
                costLevel = costLevel1;
                break;
            case 2 :
                costLevel = costLevel2;
                break;
            case 3 :
                costLevel = costLevel3;
                break;
            default:
                break;
        }

        if (postTravel.getConsumePrice() > costLevel)
            throw new MyException(20001, "超出限定金额: " + costLevel );

    }
}
